<?php 
/* ================================================================================
 * LWAdmin	 
 *
 * users_ministries.php: This is where users are assigned to ministries.
 *
 * Author: Eric Hendrickson (enhendrickson@liberty.edu)
 * Date: 2/18/05
   ================================================================================ */
	
	global $SSO, $Security, $AppID, $User, $DB;
	//Check to see if there is a valid session 
	$SessionInfo = $SSO->isValidSession("index.php?action=login", $SSO->getSessionInformation());	
	
	//Check to see if the user has rights to be on LWAdmin
	$PageRights = array('ADMIN', 'USERADMIN');
	$Security->ValidateUser($SessionInfo['username'], $AppID, $PageRights, 1);
	
	//Grab the user info
	$UserInfo = $User->GetUserInfo($SessionInfo['username']);
	
	//check to see if this a change
	if(isset($_POST['btnLeft']))
	{
		//check to make sure it has not already been inserted
		$DB->query("SELECT * FROM ministry_members WHERE username = '" . $_POST['selUsers'] . "' AND MinistryID = '" . $_POST['selNotAssigned']. "'");
		
		if($DB->get_num_rows() <= 0)
		{
			$db_string = $DB->compile_db_insert_string( 
			array (
				'Username'		=> $_POST['selUsers'],
				'MinistryID'		=> $_POST['selNotAssigned']
				));
			
			//insert the session into the sessions table
			$DB->query("INSERT INTO ministry_members (".$db_string['FIELD_NAMES'].") VALUES (".$db_string['FIELD_VALUES'].")");
		}
	}
	if(isset($_POST['btnRight']))
	{
		//check to make sure it has not already been inserted
		$DB->query("SELECT * FROM ministry_members WHERE username = '" . $_POST['selUsers'] . "' AND MinistryID = '" . $_POST['selAssigned'] . "'");
		
		if($DB->get_num_rows() > 0)
		{
			$DB->query("DELETE FROM ministry_members WHERE username = '" . $_POST['selUsers'] . "' AND MinistryID = '" . $_POST['selAssigned'] . "'");
		}
	}
	
	function GenerateNotAssignedMinistries($username)
	{
		global $DB;
		$Options = "";
		$DB->query("SELECT m.MinistryID, m.MinistryName, mm.username FROM ministries m LEFT JOIN ministry_members mm ON m.MinistryID = mm.MinistryID AND mm.username = '" . $username . "' WHERE mm.MinistryID IS NULL");
		$row = $DB->fetch_row();
		$Options = $Options . '<option selected value="' . $row['MinistryID'] . '">' . $row['MinistryName'] . '</option>';
		while($row = $DB->fetch_row())
		{	
			$Options = $Options . '<option value="' . $row['MinistryID'] . '">' . $row['MinistryName'] . '</option>';
		}
		
		return $Options;
	}

	function GenerateAssignedMinistries($username)
	{
		global $DB;
		$Options = "";
		$DB->query("SELECT m.MinistryID, m.MinistryName, mm.username FROM ministries m LEFT JOIN ministry_members mm ON m.MinistryID = mm.MinistryID WHERE mm.username ='" . $username  . "'");
		$row = $DB->fetch_row();
		$Options = $Options . '<option selected value="' . $row['MinistryID'] . '">' . $row['MinistryName'] . '</option>';
		while($row = $DB->fetch_row())
		{	
			$Options = $Options . '<option value="' . $row['MinistryID'] . '">' . $row['MinistryName'] . '</option>';
		}
		
		return $Options;
	}
?>

<table style="border-spacing:0px;border:0px;padding:0px;" width="100%">
	<tr>
		<td>
			<?php 


				if(isset($_POST['selUsers']))
				{
					$DeleteUserInfo = $User->GetUserInfo($_POST['selUsers']);

				}


			?>
			<form  action="index.php?action=users_ministries" method="post" name="frmUserMinistries" >
			 <input name="selUsers" type="hidden" value="<?php if(isset($_POST['selUsers'])) { print $_POST['selUsers'];} ?>">
			<table style="border-spacing:0px;border:0px;padding:5px;width:80%;align:center;" align="center">
				<tr>
					<td>
						<span class="TB1">Assigned Ministries</span>
					</td>
					<td>&nbsp;
					</td>
					<td>
						<span class="TB1">Avaliable Ministries</span>
					</td>
				</tr>
				<tr>
					<td valign="middle" width="150">
						<select name="selAssigned"  class="T1" multiple size="10" style="width:150px;">
							<?php print GenerateAssignedMinistries($_POST['selUsers']); ?>
						</select> 
					</td>
					<td align="center">
						<table style="border-spacing:0px;border:0px;padding:5px;align:center;">
							<tr>
								<td><input type="submit" class="B" style="width:40px;" name="btnLeft" value="<<"></td>
							</tr>
							<tr>
								<td><input type="submit" class="B" style="width:40px;" name="btnRight" value=">>"></td>
							</tr>
							<tr>
								<td><input type="button" class="B" style="width:40px;" name="btnDone" value="Done" onClick="location.href='index.php?action=users';"></td>
							</tr>
						</table>
					</td>
					<td valign="middle" width="150">
						<select name="selNotAssigned"  class="T1" multiple size="10" style="width:150px;" >
							<?php print GenerateNotAssignedMinistries($_POST['selUsers']); ?>
						</select> 
					</td>
				</tr>
			</table> 
			</form>

		</td>
	</tr>
</table>